package com.kzd.btgame.main.money

import android.view.LayoutInflater
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.LinearLayoutManager
import com.kzd.btgame.R
import com.kzd.btgame.custom.setTransitionStatusE
import com.kzd.btgame.databinding.ActivityMoneyExperienceBinding
import com.kzd.btgame.jetpack.vm.MoneyViewModel
import com.kzd.btgame.main.money.adapter.MoneyExperienceAdapter
import com.module.lemlin.app.OwnerStatusBar
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewBindingActivity
import com.module.lemlin.owner.ownerViewModel

class MoneyExperienceActivity : OwnerViewBindingActivity<ActivityMoneyExperienceBinding>() {

    private val mMoneyExperienceAdapter = MoneyExperienceAdapter()

    private val mViewModel: MoneyViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override val inflate: (LayoutInflater) -> ActivityMoneyExperienceBinding
        get() = ActivityMoneyExperienceBinding::inflate

    override fun initStatusBar(statusBar: OwnerStatusBar) {
        statusBar.run {
            addView = mViewBinding.flMoneyExperienceBar
            barColor = R.color.white
        }
        super.initStatusBar(statusBar)
    }

    override fun initView() {

        mViewBinding.includeAppRefresh.rvAppRefreshBody.apply {
            layoutManager = LinearLayoutManager(context)
        }.adapter = mMoneyExperienceAdapter

        mViewBinding.includeAppRefresh.mSmartRefreshLayout.setOnRefreshListener {
            mViewModel.moneyExperience()
        }
        mViewBinding.includeAppRefresh.mSmartRefreshLayout.setOnLoadMoreListener {
            mViewModel.moneyExperience(isLoadMore = true)
        }
    }

    override fun initData() {
        mViewModel.moneyExperience()
    }

    override fun initViewMode() {
        mViewModel.moneyExperienceLiveData.observe(this as LifecycleOwner, { response ->
            setTransitionStatusE(response,
                mMoneyExperienceAdapter,
                mViewBinding.includeAppRefresh.mSmartRefreshLayout)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            val isLoadMore = response.data?.isLoadMore
                ?: return@observe
            if (isLoadMore) {
                mMoneyExperienceAdapter.addData(data)
            } else {
                mMoneyExperienceAdapter.setNewInstance(data)
            }
        })
    }
}